package com.iss.card.view.mycard.detail;

import java.util.ArrayList;

import com.iss.card.R;
import com.iss.card.common.ApplicationManager;
import com.iss.card.common.FunctionUtil;
import com.iss.card.common.IBackKeyListener;
import com.iss.card.model.UserInfo;
import com.iss.card.view.model.MycardDetailModel;
import com.iss.card.view.model.ResponseBean;
import com.iss.card.view.mycard.CardBagModel;
import com.iss.card.view.mycard.MyCardDetailActivity;
import com.iss.card.webservice.WebServiceAsyncService;
import com.iss.card.webservice.WebServiceAsyncServiceHelper;
import com.iss.card.webservice.WebServiceUtils;
import com.iss.card.webservice.WebServiceAsyncService.Operation;
import com.iss.card.webservice.WebServiceAsyncService.PostExecute;
import com.iss.card.webservice.WebServiceManager.WebServiceRegistrationParameter;
import com.iss.card.webservice.WebServiceManager.WebServiceUrlPortManager;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CardDetailRuleDescriptionFragment extends Fragment implements IBackKeyListener{

	private static final String TAG = "CardDetailRuleDescriptionFragment";
	private CardBagModel  cardBagModel;
	private FragmentActivity mContext;
	private UserInfo usrinfInfo;
	private MycardDetailModel model;
	private TextView tvDescription;
	private String ruleDescription;
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		cardBagModel = (CardBagModel) getArguments().getSerializable(ApplicationManager.KEY_MYCARD_BRIEF_CARD);
		usrinfInfo = FunctionUtil.getCurrentLogInUser();
		mContext = (FragmentActivity) activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.card_detail_rule_description, null);
		tvDescription = (TextView) layout.findViewById(R.id.tv);
		getRuleDescription();
		return layout;
	}
	private void getRuleDescription(){
		
		String obj[] = {cardBagModel.getCardId(), usrinfInfo.getUsrMobile()};
		WebServiceAsyncService asyncService = new WebServiceAsyncService();
		asyncService.setParameterToAsyncService(mContext,
				asyncService.getNextToken(),
				Operation.OP_SERVICE_ARG_VIP_ORDER,
				WebServiceRegistrationParameter.PRODUCT_VIP_ORDER_PARAMETER,
				obj, WebServiceUrlPortManager.ACTION_URL_PORT_VIP_ORDER, this,
				WebServiceAsyncServiceHelper.TYPE_HTTP_GET_INSTREAM);
		asyncService.setPostExecute(new PostExecute() {
			@Override
			public void onAsyncServiceComplete(int token, Object envelope,
					int index) {
				if (envelope == null) {
					Log.e(TAG, "envelope is null");
					return;
				}

				ResponseBean response = WebServiceUtils
						.getObjectResultToBaseInfo(envelope,
								MycardDetailModel.class);

				ArrayList<MycardDetailModel> modelList = (ArrayList<MycardDetailModel>) response
						.getReturnList();
				if (modelList.size() != 0) {
					model = modelList.get(0);
					ruleDescription = model.getCardRuleDescription();
					tvDescription.setText(ruleDescription);
				} else {
					model = null;
				}

			}
		});
	}

	@Override
	public void cancelUpdate(Object... params) {
		// TODO Auto-generated method stub
		
	}

}
